package region;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import lombok.Data;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main {

    @Data
    public static class Entity{
        private String value;
        private String label;
        private List<Entity> children;
    }

    @Data
    public static class SysCity{
        private String id;
        private String pid;
        private String name;
        private String code;
        private String intactCode;
        private Integer level;
        public List<SysCity> children = new ArrayList<>();
    }

	/**
     * 输出到txt文件
     */
    private static void exportTxt(List<SysCity> dataList) throws IOException {
        String content = JSONUtil.toJsonStr(dataList);
        String path = "C:\\Users\\Admin\\Desktop\\target.txt";
        FileWriter fileWriter = new FileWriter(path,false);
        fileWriter.write(content);
        fileWriter.flush();
        fileWriter.close();
    }

    /**
     * 输出到excel文件
     */
    private static void exportExcel(List<SysCity> dataList) throws IOException {
        List<List<String>> rowAll = new ArrayList<List<String>>();
        List<String> row = CollUtil.newArrayList("id", "pid", "name", "code", "intact_code","level");
        rowAll.add(row);
        // 循环添加数据到excel中
        for (int i = 0; i < dataList.size(); i++) {
            SysCity city = dataList.get(i);
            List<String> rowItem = CollUtil.newArrayList(city.getId(),city.getPid(),city.getName(),city.getCode(),city.getIntactCode(),String.valueOf(city.getLevel()));
            rowAll.add(rowItem);
        }
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.setColumnWidth(-1, 30);
        writer.setRowHeight(-1,20);
        FileOutputStream output = new FileOutputStream("C:\\Users\\Admin\\Desktop\\target.xls");
        // 一次性写出内容
        writer.write(rowAll);
        writer.flush(output);
        // 关闭writer，释放内存
        writer.close();
    }

    /**
     * 输出到json文件
     */
    private static void exportJson(List<SysCity> dataList) throws IOException {
    	// 输出的json需要有层级结构，所以需要将dataList构建为树结构
        List<SysCity> list = TreeUtil.buildTree(dataList, "0", "pid");
        String content = JSONUtil.toJsonStr(list);
        String path = "C:\\Users\\Admin\\Desktop\\target.json";
        FileWriter fileWriter = new FileWriter(path,false);
        fileWriter.write(content);
        fileWriter.flush();
        fileWriter.close();
    }

    public static void main(String[] args) throws IOException {
        List<Entity> list = readSourceTxt();
        List<SysCity> dataList = new ArrayList<>();
        int id = 1;
        String pid="0";
        getCityList(list,dataList,id,pid);
        // 生成txt文件
        exportTxt(dataList);
        // 生成excel
        exportExcel(dataList);
        // 生成json文件
        exportJson(dataList);
    }

    /**
     * 读取源文件，解析为json格式，添加到list中
     */
    private static List<Entity> readSourceTxt() throws IOException {
        String path = System.getProperty("user.dir")+"\\src\\test\\java\\com\\source.txt";
        File file = new File(path);
        BufferedReader br = new BufferedReader(new FileReader(file));
        StringBuilder sb = new StringBuilder();
        String st;
        while ((st = br.readLine()) != null) {
            sb.append(st);
        }
        return JSONUtil.toList(sb.toString(), Entity.class);
    }

    /**
     * 数据处理
     * @param list 原始数据
     * @param dataList 目标数据
     * @param id id
     * @param pid 父级id
     */
    public static int getCityList(List<Entity> list,List<SysCity> dataList,int id,String pid){
        for (Entity entity : list) {
            SysCity city = new SysCity();
            city.setId(String.valueOf(id));
            city.setName(entity.getLabel());
            city.setIntactCode(entity.getValue());
            String code = entity.getValue();
            //源数据的编码都是六位数，省级后面四位都是0，去掉；市级后面两位是0，去掉；县（区）级取全部
            if (code.endsWith("0000")){
                code = code.replace("0000","");
                city.setLevel(1);
            }else if (code.endsWith("00")){
                code = code.substring(0,4);
                city.setLevel(2);
            }else {
                city.setLevel(3);
            }
            //code只取最后两位，完整的编码前面几位都是由它的父级组成的，intactCode存储的是完整的编码
            city.setCode(code.substring(code.length()-2));
            city.setPid(pid);
            dataList.add(city);
            id++;
            List<Entity> children = entity.getChildren();
            if (children != null && !children.isEmpty()){
                id = getCityList(children,dataList,id, city.getId());
            }
        }
        return id;
    }
}
